package com.itheima.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.system.IMenuRepositroy;
import com.itheima.bos.domain.system.Menu;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.IMenuService;
@Service
@Transactional
public class MenuServiceImpl implements IMenuService {
	
	@Autowired
	private IMenuRepositroy menuRepositroy;

	@Override
	public List<Menu> findAll() {
		
		return menuRepositroy.findAll();
	}

	@Override
	public void save(Menu model) {
		//防止用户没有选中父菜单
		if (model.getParentMenu()!=null && model.getParentMenu().getId()==0) {
			model.setParentMenu(null);
		}
		//保存菜单
		menuRepositroy.save(model);
	}
	
	//根据用户登录展示所选的菜单
	@Override
	public List<Menu> findByUser(User user) {
		
		//判断当前用户是否是超级关联员
		if (user.getUsername().equals("admin")) {
			return menuRepositroy.findAll();
		}{
			return menuRepositroy.findByUser(user.getId());
		}
	}

}
